(function(){
    function GetUnTicket(){
        this.init();
     
       
        
        

    }

    GetUnTicket.prototype = {
        constructor:GetUnTicket,
        init:function(){
            this.nodata = '<tr><td  colspan="9">暂无信息</td></tr>';
            var _this = this;
            this.getUnticketList(1);
            $.extend({page:function(pn){
                _this.getUnticketList(pn);
            }});

            // 日历
            $.datetimepicker.setLocale('ch');
            $('#endtime').datetimepicker({
                format: 'Y-m-d',
                value:new Date().dateadd("d", 1),
                maxDate: new Date(),
                onChangeDateTime:function(dp,$input){
                    var limitStart = new Date($('#begintime').val().replace('-','/'));
                    var nowStart = new Date($('#endtime').val().replace('-','/'));
                    if(limitStart > nowStart){
                        $('#endtime').val(limitStart.format("YY-M-D"))

                    }
                },
                timepicker: false
            });
            
            $('#begintime').datetimepicker({
                format: 'Y-m-d',
                value:new Date().dateadd("d", -1),
                maxDate: new Date(),
                onChangeDateTime:function(dp,$input){
                    var limitStart = new Date($('#endtime').val().replace('-','/'));
                    var nowStart = new Date($('#begintime').val().replace('-','/'));
                    if(limitStart < nowStart){
                        $('#begintime').val(limitStart.format("YY-M-D"))

                    }
                    
                },
                timepicker: false
            });
            







        },
        getUnticketList:function( pn ){
            var _this = this;
            $.ajax({
                type:"get",
                url:"/sys?fid=101&state=12&ps=10&pn="+pn,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        var data = info.data;
                        var count = info.count;
                        // state  -1撤单、0.未分配、1.已分配、2.已接单
                        var stateArr = ["撤单","未分配","未分配","未分配"];
                        if( data.length )
                        {
                            var strArr = [];
                            data.forEach(function( value, key  ){
                                var ticketid = value.ticketid;
                                var orderid = value.orderid;
                                var ctime = value.createtime.substring(0,19);
                                var etime = value.endtime.substring(0,19);
                                var siteid = value.siteid ? value.siteid : "";
                                var stateIndex = value.state + 1;
                                var state = stateArr[stateIndex];
                                var mulity = value.mulity;
                                var tmoney = value.tmoney;
                                var codes = value.codes;
                                var trClass =  key%2 == 0 ? "even" : "odd";
                                var html = '<tr class="'+trClass+'"  id="tr'+ticketid+'">\
                                                <td>'+ticketid+'</td>\
                                                <td>'+orderid+' </td>\
                                                <td>'+ctime+'</td>\
                                                <td>'+etime+'</td>\
                                                <td>'+siteid+'</td>\
                                                <td>'+state+'</td>\
                                                <td>'+mulity+'</td>\
                                                <td>'+tmoney+'</td>\
                                                <td>'+codes+'</td>\
                                                <td><a  href="javascript:;"  class="btn btn-info  cancelBtn"  data-tid="'+ticketid+'">撤单</a></td>\
                                            </tr>';

                                    strArr.push( html );

                            });

                            $("#unticketList").html( strArr.join(""));
                            // 撤单
                            $(".cancelBtn").unbind("click").click(function(){
                                var tid = $(this).data("tid");
                                $("#hiddenTid").val(tid);
                                $("#cancelModal").modal();

                            });
                            $("#confirmBtn").unbind("click").click(function(){
                                _this.cancelTicket();
                            });




                            // 分页
                            var pn = count.pn;
                            var tp = count.tp;
                            var tr = count.tr;
                            $("#countpages").html( tp );
                            $("#countnums").html( tr );
        
                            var pageHtml = getPages(pn,tp, "$.page");

                            $("#paginationSel").html(pageHtml);



                        }
                        else
                        {
                            $("#unticketList").html(_this.nodata);

                        }

                    }
                    else
                    {
                        if( code == 2 )
                        {
                            location.href = "login.html";

                        }
                        else
                        {
                            $("#tipsContent").html( desc );
                            $("#loginoutModal").modal();

                        }
                    }


                }
            });


        },
        // 撤单
        cancelTicket:function(){
            var tid = $("#hiddenTid").val();
            $.ajax({
                type:"get",
                url:"/sys?fid=102&ticketid="+tid,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        $("#tr"+tid).remove();
                        $("#cancelModal").modal("hide");
                    }
                    else
                    {
                        $("#cancelModal").modal("hide");
                        if( code == 2 )
                        {
                            $("#myModalLabel").html("你还未登录");
                            $("#loginoutModal").modal();
                            setTimeout(function(){
                                location.href = "login.html";
                            }, 1000);
                        }
                        else
                        {
                            $("#myModalLabel").html(desc);
                            $("#loginoutModal").modal();

                        }

                    }


                }
            });


        }
      
      
    };





  new GetUnTicket();

}());